package twi.query;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import twi.OWLModel;
import twi.Storable;
import twi.filter.AnonFilter;
import twi.filter.Filter;
import twi.filter.NodeFilter;

public abstract class Query implements Storable{

	protected OWLModel model;
	protected ArrayList<Filter> filter = new ArrayList<Filter>();
	private ArrayList<NodeFilter> nodeFilter = new ArrayList<NodeFilter>();
	protected int transitivity = 0;
	protected boolean transitivityBevorFilter = true;
	
	public Query(OWLModel model){
		this();
		this.model = model;
	}
	protected Query(){
		filter.add(new AnonFilter());
		
	}
		
	public abstract String getSPARQLString();
	
	public static String createPrefixDeclarations(OWLModel model) {
        String queryString = "";
        queryString += "BASE <" + model.getBasePrefix().substring(0, model.getBasePrefix().length()-1)+">\n";
        queryString += "PREFIX : <"+model.getBasePrefix()+">\n";
        Map prefmap = model.getNsPrefixMap();
        Iterator iter = prefmap.keySet().iterator();
        while(iter.hasNext()){
        	String key = (String) iter.next();
        	if (key.length() == 0 || key.equals(" "))
        		continue;
        	String val = (String) prefmap.get(key);
        	queryString += "PREFIX " + key + ": <"+val+">\n";
        }
        return queryString;
    }
	public OWLModel getModel() {
		return model; 
	}	
	public ArrayList<Filter> getFilter() {
		return filter;
	}
	public void addFilter(Filter f) {
		if (f != null)
			this.filter.add(f);
	}
	public int getTransitivity() {
		return transitivity;
	}
	public void setTransitivity(int transitivity) {
		this.transitivity = transitivity;
	}
	public boolean isTransitivityBevorFilter() {
		return transitivityBevorFilter;
	}
	public void setTransitivityBevorFilter(boolean transitivityBevorFilter) {
		this.transitivityBevorFilter = transitivityBevorFilter;
	}
	public ArrayList<NodeFilter> getNodeFilter() {
		return this.nodeFilter;
	}
	public void addNodeFilter(NodeFilter nf){
		if (nf != null)
			nodeFilter.add(nf);
	}
}
